﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using DesktopToyWpf.ViewModels;

namespace DesktopToyWpf.Views;

public partial class HappyButton
{
    public HappyButton()
    {
        InitializeComponent();
    }

    private Brush BaseBorderColor;
    private Brush HighlightBorderColor;
    private Brush HoverBorderColor;

    private MainViewModel ViewModel;
    
    private DispatcherTimer LongPressTimer;
    private DispatcherTimer HappyTimer; // 让心情计数器自动加1
    private DispatcherTimer SaveTimer; // 用于防止频繁保存

    private bool IsHappyCountChanged;
    
    private void HappyButton_OnLoaded(object sender, RoutedEventArgs e)
    {
        ViewModel = DataContext as MainViewModel;
        
        BaseBorderColor = FindResource("BaseBorderColor") as Brush ?? throw new NullReferenceException();
        HighlightBorderColor = FindResource("HighlightBorderColor") as Brush ?? throw new NullReferenceException();
        HoverBorderColor = FindResource("HoverBorderColor") as Brush ?? throw new NullReferenceException();

        LongPressTimer = new DispatcherTimer(DispatcherPriority.Normal)
        {
            Interval = TimeSpan.FromMilliseconds(500)
        };
        
        LongPressTimer.Tick += LongPressTimer_OnTick;
        
        HappyTimer = new DispatcherTimer(DispatcherPriority.Normal)
        {
            Interval = TimeSpan.FromMilliseconds(100)
        };
        
        HappyTimer.Tick += HappyTimer_OnTick;

        SaveTimer = new DispatcherTimer(DispatcherPriority.Normal)
        {
            Interval = TimeSpan.FromMilliseconds(1000)
        };
        SaveTimer.Tick += SaveTimer_OnTick;
    }

    private void HappyTimer_OnTick(object? sender, EventArgs e)
    {
        ViewModel.HappyCount++;
    }

    private void LongPressTimer_OnTick(object? sender, EventArgs e)
    {
        if (!HappyTimer.IsEnabled)
        {
            HappyTimer.Start();
        }
    }

    private void Border_OnMouseEnter(object sender, MouseEventArgs e)
    {
        Border.BorderBrush = HoverBorderColor;
        Border.BorderThickness = new Thickness(4);
        Icon.Opacity = 0.7d;
    }

    private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton != MouseButton.Left) return;
        
        Border.BorderBrush = HighlightBorderColor;
  
        ViewModel.HappyCount++;
        LongPressTimer.Start();
        IsHappyCountChanged = true;
    }

    private void Border_OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton != MouseButton.Left) return;
        
        if (IsMouseOver)
        {
            Border.BorderBrush = HoverBorderColor;
        }
        else
        {
            Border.BorderBrush = BaseBorderColor;
        }
        
        LongPressTimer.Stop();
        HappyTimer.Stop();
        SaveIfNeed();
    }

    private void Border_OnMouseLeave(object sender, MouseEventArgs e)
    {
        Border.BorderBrush = BaseBorderColor;
        Border.BorderThickness = new Thickness(2);
        Icon.Opacity = 0.5d;
        LongPressTimer.Stop();
        HappyTimer.Stop();
        SaveIfNeed();
    }

    
    private void SaveTimer_OnTick(object? sender, EventArgs e)
    {
        Debug.WriteLine("HappyCount延迟保存");
        Global.SaveStorage();
        SaveTimer.Stop();
    }
    
    private void SaveIfNeed()
    {
        if (!IsHappyCountChanged) return;
        IsHappyCountChanged = false;

        if (SaveTimer.IsEnabled) return;
        SaveTimer.Start();
    }
}